package br.edu.ifrn.dominio;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mailer {
	//Mailer.sendMail(user, user, "AQUI", "MSG");
	
    public static void sendMail(Usuario userFrom, String userTo, String mailTitle, String mailMessage) throws Exception{
    	Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
		props.put("mail.debug", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.localhost", "gmail.com");
		
		Session s = Session.getInstance(props, null);
		s.setDebug(true);
		
		MimeMessage message = new MimeMessage(s);
		
		InternetAddress from = new InternetAddress(userFrom.getEmail(), "CICC - "+userFrom.getNome());
		InternetAddress to = new InternetAddress(userTo);
		
		message.setSentDate(new Date());
		message.setFrom( from );
		message.addRecipient(Message.RecipientType.TO, to);
		
		message.setSubject(mailTitle);
		message.setContent(mailMessage, "text/plain");
		
		Transport tr = s.getTransport("smtp");			
		tr.connect("smtp.gmail.com", "ifrncicc@gmail.com", "cicc1234");
		message.saveChanges();
		tr.sendMessage(message, message.getAllRecipients());
		tr.close();
    }
}
